package TwoAgain;

import Two.Emp;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame extends JFrame {
    private MyText nameText = new MyText("姓名",120,100,this);
    private MyText ageText = new MyText("年龄",120,200,this);
    private MyText phNumText = new MyText("电话",120,300,this);
    private MyText sexText = new MyText("性别",120,350,this);

    private IUse use = new Accompli();


    public MyJFrame(){
        this.setLayout(null);

        this.init();

        this.setSize(600,600);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){
        MyButton add = new MyButton("添加",100,400,this);
        MyButton show = new MyButton("显示",250,400,this);
        MyButton search = new MyButton("查找",400,400,this);


        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addOption();
            }
        });

        show.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showOption();
            }
        });

        search.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                searchOption();
            }
        });
    }

    private void addOption(){
        Employee employee = new Employee(nameText.getText(),ageText.getText(),sexText.getText(),phNumText.getText());
        if (this.check().length()==0) {
            JOptionPane.showMessageDialog(null,"添加成功");
            use.add(employee);
        }else {
            JOptionPane.showMessageDialog(null,this.check());
        }
    }
    private String check(){
        String nameCheck = "[\u4e00-\u9fa5a-zA-Z]{2,}";
        String ageCheck = "[0-9][0-9]";
        String phNumCheck = "1[3578][0-9]{9}";

        String information = "";
        if (nameText.getText().matches(nameCheck)==false){
            information += "名字必须为两位以上的字母或汉字"+"\n";
        }
        if (ageText.getText().matches(ageCheck)==false){
            information += "年龄必须为1-2位数字"+"\n";
        }
        if (phNumText.getText().matches(phNumCheck)==false){
            information += "电话必须为13、15、17、18开始的11位数"+"\n";
        }

        return information;
    }

    private void showOption(){
        System.out.println(use.show());
    }

    private void searchOption(){
        Employee e = use.search(nameText.getText());
        if (e!=null){
            ageText.setText(e.getAge());
            sexText.setText(e.getSex());
            nameText.setText(e.getName());
            phNumText.setText(e.getPhNum());
        }else {
            JOptionPane.showMessageDialog(null,"查无此人");
        }
    }




    public static void main(String[] args) {
        new MyJFrame();
    }
}
